package com.mnxc.gxa.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.mnxc.gxa.dto.UserDTO;
import com.mnxc.gxa.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;
import java.util.function.Supplier;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill");
        this.strictInsertFill(metaObject,"createDate", LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"createTime", LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"updateDate", LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"updateTime", LocalDateTime.class,LocalDateTime.now());
        UserDTO user = UserHolder.getUser();
        if(user==null){
            log.warn("未登录就插入数据信息，无法添加创建者和更新者。");
            return;
        }
        this.strictInsertFill(metaObject,"createBy",Long.class, user.getId());
        this.strictInsertFill(metaObject,"updateBy",Long.class, user.getId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill");
        this.strictUpdateFill(metaObject,"updateDate", LocalDateTime.class,LocalDateTime.now());
        this.strictUpdateFill(metaObject,"updateTime", LocalDateTime.class,LocalDateTime.now());
        UserDTO user = UserHolder.getUser();
        if(user==null){
            log.warn("未登录就更新数据信息，无法添加更新者。");
            return;
        }
        this.strictUpdateFill(metaObject,"updateBy",Long.class, user.getId());
    }


}
